{extend name="main" /}

{block name="body"}
{include file="breadcrumb" /}
<div class="layui-fluid">
    <div class="layui-card">
        <div class=" layui-card-header layuiadmin-card-header-auto">
            {include file="new_module/index_form"}
        </div>
        <div class="layui-card-body">
            <table class="layui-hide layui-table" id="test"  lay-filter="test"></table>
        </div>
    </div>


    <!--引入渲染模板-->
    {include file="new_module/index_tpl"}
</div>
{/block}
{block name="js"}
<script src="/static/admin/js/layuiTableApi.js"></script>
<script>
        var table = layui.table,form = layui.form,laytpl = layui.laytpl;
        var cols = [
            {field:'table_name', title:'表名',templet: "#showTips"},
            {field:'table_comment', title:'表说明',}
            ,{field:'name', title:'模块名'}
            ,{field:'file_number', title: '字段数', sort: true}
            ,{field:'file_name', title:'文件名'}
            ,{field:'output_time', title:'生成时间',}
            ,{field:'status', title:'是否生成菜单',  templet: '#switchTpl', unresize: true}
            ,{field:'is_model', title:'是否隐藏',  templet: '#isHideSwitchTpl', unresize: true}
            ,{field:'is_model', title:'仅创建模型',  templet: '#isModelSwitchTpl', unresize: true}
            ,{ title:'操作', templet: '#apiListBar',width:250, unresize: true}
        ];
        //表格初始化
        table.render(getLayuiInit('#test','index.html',cols));

        //监听单行工具条
        table.on('tool(test)',(obj)=>{
            var data = obj.data;
            // console.log(data,'--data')
            //创建
            if(obj.event === 'create'){
                generate(data);
            } else if(obj.event === 'edit'){
                //渲染并加载模板
                ajax_open('模块编辑','edit.html?id='+data.id,'80%','80%',false)
            } else if(obj.event ==='delete'){
                $.post('delete',{id:data.id},(res)=>{
                    if(res.code==1){
                        layer.msg(res.msg,{icon:1,time:1000},()=>{table.reload('test',{})});
                    }else{
                        layer.alert(res.msg,{icon:5});
                    }
                })
            } else if(obj.event === 'create_api'){
                generate(data,1);
            }else if(obj.event === 'delete_api'){
                $.post('delete',{id:data.id,isApi:1},(res)=>{
                    if(res.code==1){
                        layer.msg(res.msg,{icon:1,time:1000},()=>{table.reload('test',{})});
                    }else{
                        layer.alert(res.msg,{icon:5});
                    }
                })
            }
        });

        //表格顶部工具栏事件
        table.on('toolbar(test)', (obj)=>{
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch(obj.event){
                //同步数据
                case 'sync':
                    _get('syncTable',{},(msg)=>{
                        table.reload('test',{})
                    })
                    break;
            };
        });

        //触发排序事件
        table.on('sort(test)', OnLayuiOrder);


        //监听状态操作
        form.on('switch(switchTpl)', OnLayuiSwitch);

        //生成模块
        function generate(tableData,isApi,isCover) {
            var id = tableData.id
            isCover = isCover ||0
            isApi = isApi ||0
            var data = {id:id,isCover:isCover,isApi:isApi};
            $.post('generate',data,(res)=>{
                if(res.data =='is_existence'){
                    data.msg = res.msg
                    if(tableData.is_model==1){
                        layer.confirm(res.msg, {
                            btn: ['覆盖生成','取消'] //按钮
                        }, function(){
                            generate(tableData,isApi,1);
                        }, function(){});
                    }else{
                        coverMsgF(data)
                    }
                }else{
                    if(res.code==1){
                        layer.msg(res.msg,{icon:1,time:1000},()=>{table.reload('test',{})});
                    }else{
                        layer.alert(res.msg,{icon:5,title:'生成失败'});
                    }
                }
            })
        }

        var coverIndex;
        //弹出询问框
        function coverMsgF(data) {
            data.isCover = 1;
            laytpl(coverMsg.innerHTML).render(data, function(html){
                coverIndex = layer.open({
                    type: 1,
                    title:'文件覆盖提示',
                    skin: 'layui-layer-black', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: html
                });
                form.render();
            });
        }
        //提交
        function coverConfirm() {
            var data = $('#coverMsgForm').serialize();
            _post('generate',data,(res)=>{
                layer.msg(res.msg,{icon:1})
                layer.close(coverIndex);
            })
        }
        //取消
        function coverCancel() {
            layer.close(coverIndex);
        }


</script>
{/block}

